var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var bodyParser = require("body-parser");
var https = require('https');
var config = require("../config.json");
var pool = mysql.createPool(config.mysql);
router.use(bodyParser.json({
	limit: "20000 kb"
}));
router.use(bodyParser.urlencoded({
	parameterLimit: 100000,
	limit: '50mb',
	extended: false
}));
/* GET users listing. */
router.get('/allRelease', function(req, res, next) {
	pool.getConnection(function(err, connection) {
		connection.query('select id,title,url from news', function(err, rows, fields) {

			if (err) {
				console.log(err);
			};
			var json = {
				data: rows
			}
			res.send(json);
			connection.release();
		});

	});

});
router.post('/toRelease', function(req, res) {
	var title = "'" + req.body.title + "'";
	var link_url = "'" + req.body.link_url + "'";
	var sql = "INSERT INTO news (title,url) VALUES (" + title + "," + link_url + ")";
	pool.getConnection(function(err, connection) {
		connection.query(sql, function(err, rows, fields) {
			if (err) throw err;
			var json = {
				data: "发布成功"
			}
			res.send(json);
			connection.release();
		});
	});
});
/* 删除 */
router.get('/del/:id', function(req, res, next) {
	pool.getConnection(function(err, connection) {
		connection.query('DELETE  from news where id=' + req.params.id + '', function(err, rows, fields) {
			if (err) {
				console.log(err);
			};

			var json = {
				data: rows
			}
			res.send(json);
			connection.release();
		});
	});
});


module.exports = router;